package com.fengpanlong.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fengpanlong.entity.EmployeeProject;
import com.fengpanlong.entity.Idea;
import com.fengpanlong.entity.IdeaComment;
import com.fengpanlong.service.IdeaService;
import com.fengpanlong.service.MessageService;
import com.fengpanlong.service.PagerService;
import com.fengpanlong.util.Form;
import com.fengpanlong.util.IdeaHelp;
import com.fengpanlong.util.Pager;
import com.fengpanlong.util.StringUtil;


public class IdeaServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getMethod();
		String m = request.getParameter("_m");
		if("GET".equalsIgnoreCase(method)) {
			if("newIdea".equals(m)) {
				newIdea(request,response);
			} else if("showIdea".equals(m)) {
				showIdeaComment(request,response);
			} else if("delIdeaComment".equals(m)) {
				delIdeaComment(request,response);
			} else if("delIdea".equals(m)) {
				delIdea(request,response);
			} else if("editIdea".equals(m)) {
				editIdea(request,response);
			} else {
				goToIdeaJsp(request,response);
			}
		} else {
			if("saveIdea".equals(m)) {
				saveIdea(request,response);
			} else if("saveIdeaComment".equals(m)){
				saveIdeaComment(request,response);
			} else if("editIdea".equals(m)) {
				editIdea(request,response);
			}
		}
 	}
	/**
	 * 编辑想法
	 * @param request
	 * @param response
	 */
	private void editIdea(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String method = request.getMethod();
		String ideaId = request.getParameter("ideaId");
		Idea idea = IdeaService.findIdeaByIdeaId(ideaId);
		if("GET".equalsIgnoreCase(method)) {
			request.setAttribute("idea", idea);
			goTojsp(request, response, "editIdea.jsp");
		} else {
			Form.saveParam(request, idea);
			IdeaService.update(idea);
			goToIdeaJsp(request, response);
		}
		
	}
	/**
	 * 删除想法
	 * @param request
	 * @param response
	 */
	private void delIdea(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String ideaId = request.getParameter("ideaId");
		Idea idea = IdeaService.findIdeaByIdeaId(ideaId);
		IdeaService.delIdeaByIdeaId(ideaId);
		String msgContent =" 删除了标题为" + idea.getTitle() + "的想法。";
		MessageService.sendMessage(request,msgContent);
		goToIdeaJsp(request, response);
	}
	/**
	 * 发表新评论
	 * @param request
	 * @param response
	 */
	private void saveIdeaComment(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		EmployeeProject ep = (EmployeeProject)request.getSession().getAttribute("ep");
		
		String ideaId = request.getParameter("ideaId");
		
		IdeaComment ideaComment = new IdeaComment();
		Form.saveParam(request, ideaComment);
		
		ideaComment.setId(StringUtil.getId());
		ideaComment.setCreatetime(StringUtil.formateTime());
		ideaComment.setIdeaId(ideaId);
		ideaComment.setEmployeeId(ep.getEmployeeId());
		IdeaService.saveIdeaComment(ideaComment);
		Idea idea = IdeaService.findIdeaByIdeaId(ideaId);
		String msgContent =" 发表了对标题为" + "<a href = \"idea.jspx?_m=showIdea&ideaId=" + idea.getId() + "\">" + idea.getTitle() + "</a>的想法的评论。";
		MessageService.sendMessage(request,msgContent,ideaComment.getCreatetime());
		showIdeaComment(request, response);
	}
	/**
	 * 通过想法评论id删除想法评论
	 * @param request
	 * @param response
	 */
	private void delIdeaComment(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String ideaCommentId = request.getParameter("ideaCommentId");
		Idea idea = IdeaService.findIdeaByIdeaCommentId(ideaCommentId);
		IdeaService.delIdeaCommentByIdeaCommentId(ideaCommentId);
		String msgContent =" 删除了对标题为"  + "<a href = \"idea.jspx?_m=showIdeaComment&ideaId=" + idea.getId() + "\">" + idea.getTitle() + "</a>的想法的评论。";
		MessageService.sendMessage(request,msgContent);
		showIdeaComment(request, response);
	}
	/**
	 * 显示想法
	 * @param request
	 * @param response
	 */
	private void showIdeaComment(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String ideaId = request.getParameter("ideaId");
		IdeaHelp ideaHelp = IdeaService.getIdeaHelpByIdeaId(ideaId);
		if(ideaHelp == null) {
			response.sendRedirect("idea.jspx");
			return;
		}
		request.setAttribute("ideaHelp", ideaHelp);
		goTojsp(request, response, "showIdea.jsp");
	}
	/**
	 * 保存想法
	 * @param request
	 * @param response
	 */
	private void saveIdea(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		
		Idea idea = new Idea();
		Form.saveParam(request, idea);
		idea.setId(StringUtil.getId());
		idea.setCreatetime(StringUtil.formateTime());
		
		EmployeeProject ep = (EmployeeProject)request.getSession().getAttribute("ep");
		idea.setEmployeeId(ep.getEmployeeId());
		idea.setProjectId(ep.getProjectId());
		IdeaService.save(idea);
		String msgContent =" 发表了标题为" + "<a href = \"idea.jspx\">" + idea.getTitle() + "</a>的想法。";
		MessageService.sendMessage(request,msgContent,idea.getCreatetime());
		goToIdeaJsp(request, response);
	}
	/**
	 * 新建想法
	 * @param request
	 * @param response
	 */
	private void newIdea(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		goTojsp(request, response, "newIdea.jsp");
	}
	/**
	 * 去想法页面
	 * @param request
	 * @param response
	 */
	private void goToIdeaJsp(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String pageNum = request.getParameter("p");
		int num  = 1;
		
		if(StringUtil.isNotEmpty(pageNum)) {
			try {
				num = Integer.parseInt(pageNum);
			} catch (NumberFormatException e) {
				response.sendRedirect("idea.jspx？p=1");
				return;
			}
		}
		EmployeeProject ep = (EmployeeProject)request.getSession().getAttribute("ep");
		Pager<IdeaHelp> pager = PagerService.findPagerOfProjectsByProjectId(num, ep.getProjectId());
		request.setAttribute("pager", pager);
		goTojsp(request, response, "idea.jsp");
	}
	/**
	 * 去jsp页面
	 * @param request
	 * @param response
	 * @param jspName
	 * @throws ServletException
	 * @throws IOException
	 */
	private void goTojsp(HttpServletRequest request,
			HttpServletResponse response,String jspName) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/views/" + jspName).forward(request, response);		
	}
}
